home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_04 / cornel / mtchwild.c < prev    next >
Text File  |  1995-01-17  |  2KB  |  68 lines

  1. Listing 1
  2. ---------
  3.  
  4. /* -----------------------------------------------------------------------
  5.  
  6.     int MatchWild (const char * wildstr, const char * candstr)
  7.  
  8.     Match candidate string to wildcard string containing any number
  9.     of '*' or '?' wildcard characters.  The '*' matches any number of
  10.     characters, including zero characters.  The '?' matches exactly
  11.     one character.  Returns 1 if there is a match, else 0.
  12.  
  13.     Benchmarks for Pentium/60:
  14.  
  15.       WILDCARD STRING        CANDIDATE STRING          Microsecs.
  16.         asdfghjkl              asdfghjkl                  1.5
  17.         *asdfgh                XXXasdfgh                  2.0
  18.         asdfgh*                asdfghXXX                  1.8
  19.         *aaa*bbb*ccc*          XaaaXXbbbcccXXX            4.1
  20.         asd??fgh??*            asdXXfghXX                 2.7
  21.         *aa??a*bbb             XXaaXXaXXbbXaaXXabbb       5.5
  22.  
  23. */
  24.  
  25.  
  26. int MatchWild(const char * pWild, const char * pString)
  27. {
  28.    int   ii, star;
  29.  
  30. new_segment:
  31.  
  32.    star = 0;
  33.    while (pWild[0] == '*')
  34.    {
  35.       star = 1;
  36.       pWild++;
  37.    }
  38.  
  39. test_match:
  40.  
  41.    for (ii = 0; pWild[ii] && (pWild[ii] != '*'); ii++)
  42.    {
  43.       if (pWild[ii] != pString[ii])
  44.       {
  45.          if (! pString[ii]) return 0;
  46.          if (pWild[ii] == '?') continue;
  47.          if (! star) return 0;
  48.          pString++;
  49.          goto test_match;
  50.       }
  51.    }
  52.  
  53.    if (pWild[ii] == '*')
  54.    {
  55.       pString += ii;
  56.       pWild += ii;
  57.       goto new_segment;
  58.    }
  59.  
  60.    if (! pString[ii]) return 1;
  61.    if (ii && pWild[ii-1] == '*') return 1;
  62.    if (! star) return 0;
  63.    pString++;
  64.    goto test_match;
  65. }
  66.  
  67.  
  68.